home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / zm16src.lzh / FILEIO.C < prev    next >
C/C++ Source or Header  |  1988-05-19  |  2KB  |  143 lines

  1. /*
  2.  *     File I/O (with large buffers) Module
  3.  *
  4.  *        Jwahar Bammi
  5.  *            usenet: mandrill!bammi@{decvax,sun}.UUCP
  6.  *            csnet:  bammi@mandrill.ces.CWRU.edu
  7.  *            arpa:   bammi@mandrill.ces.CWRU.edu
  8.  *            CompuServe: 71515,155
  9.  */
  10.  
  11. #include "config.h"
  12.  
  13. #include "zmdm.h"
  14. #include "common.h"
  15.  
  16. #define    O_RDONLY    1
  17. #define O_WRONLY    2
  18. #define O_APONLY    4
  19. #ifndef DYNABUF
  20. #define MBUFSIZ        (((long)BBUFSIZ)-1L)
  21. #else
  22. static long MBUFSIZ;
  23. #endif /* DYNABUF */
  24.  
  25. static unsigned char *bptr;
  26.  
  27. static long flcount = (-1L);
  28. static int bufmode;
  29.  
  30. int stfopen(name, mode)
  31. char *name, *mode;
  32. {
  33.     register int handl;
  34.  
  35.     switch(*mode)
  36.     {
  37.         case 'r':
  38.         if((handl = Fopen(name, 0)) <= 0)
  39.             return -1;
  40.         bufmode = O_RDONLY;
  41.         break;
  42.  
  43.         case 'w':
  44.         if((handl = Fcreate(name, 0)) <= 0)
  45.         {
  46.             if((handl = Fopen(name, 1)) <= 0)
  47.                 return -1;
  48.         }
  49.         bufmode = O_WRONLY;
  50.         break;
  51.  
  52.         case 'a':
  53.         if((handl = Fopen(name, 2)) <= 0)
  54.             return -1;
  55.         Fseek(0L, handl, 2);
  56.         bufmode = O_APONLY;
  57.         break;
  58.  
  59.        default:
  60.         return -1;
  61.     }
  62.  
  63. #ifdef DYNABUF
  64.     MBUFSIZ = BBUFSIZ - 1L;
  65. #endif /* DYNABUF */
  66.  
  67.     bptr = bufr;
  68.     flcount = (-1L);
  69.     return handl;
  70. }
  71.  
  72. stfclose(handl)
  73. int handl;
  74. {
  75.     if(bufmode == O_RDONLY)
  76.         return Fclose(handl);
  77.     if(stflush(handl))
  78.     {
  79.         Fclose(handl);
  80.         return -1;
  81.     }
  82.     return Fclose(handl);
  83. }
  84.  
  85. stputc(c, handl)
  86. unsigned int c;
  87. int handl;
  88. {
  89.     if(flcount >= MBUFSIZ)
  90.     {
  91.         if(Fwrite(handl, (long)BBUFSIZ, bufr) != (long)BBUFSIZ)
  92.             return -1;
  93.         flcount = (-1L);
  94.         bptr  = bufr;
  95.     }
  96.     flcount++;
  97.     *bptr++ = c;
  98.     return 0;
  99. }
  100.  
  101. stgetc(handl)
  102. int handl;
  103. {
  104.     if(flcount <= 0)
  105.     {
  106.         if((flcount = Fread(handl, (long)BBUFSIZ, bufr)) == 0)
  107.             return EOF;
  108.         bptr = bufr;
  109.     }
  110.     flcount--;
  111.     return(*bptr++);
  112. }
  113.  
  114. stflush(handl)
  115. int handl;
  116. {
  117.     if(flcount < 0)
  118.         return 0;
  119.  
  120.     if(Fwrite(handl, (long)(flcount+1L), bufr) != (flcount+1L))
  121.         return -1;
  122.     flcount = (-1L);
  123.     bptr = bufr;
  124.     return 0;
  125. }
  126.         
  127. stfseek(handl, disp, mode)
  128. int handl;
  129. long disp;
  130. int  mode;
  131. {
  132.     if(bufmode != O_RDONLY)
  133.         if(stflush(handl))
  134.             return -1;
  135.     Fseek(disp, handl, mode);
  136.     flcount = (-1L);
  137.     bptr = bufr;
  138.     return 0;
  139. }
  140.  
  141.  
  142. /* -eof- */
  143.